﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LimitedScopeTrader
{
    public partial class MDIWindow : Form
    {
        const int TAB_ADD_SECURITY = 0;
        const int TAB_ENTER_TRADE = 1;
        const int TAB_TICKER_LOOKUP = 2;
        const int TAB_ENTER_TRANSACTION = 3;
        const int TAB_TICKER_RESEARCH = 4;

        public MDIWindow()
        {
            InitializeComponent();
            frmLogon logon = new frmLogon();
            logon.MdiParent = this;
            logon.Show();
        }

        private void LogOut()
        {
            LSTApplication.User = null;
            foreach (Form childForm in this.MdiChildren)
            {
                childForm.Close();
            }
        }

        #region Menu Event Handlers
        /// <summary>
        /// Menu option for exiting the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        /// <summary>
        /// Menu option for logging into the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void logInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogon loginForm = new frmLogon();
            loginForm.MdiParent = this;
            loginForm.Show();
        }

        /// <summary>
        /// /Menu option for logging out of the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LogOut();
        }

        /// <summary>
        /// Menu option for launching the chart form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void performanceHistoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Need to ensure only one instance of the chart form is open.
            bool open = false;
            foreach (Form child in this.MdiChildren)
            {
                if (child is frmChart)
                {
                    open = true;
                    break;
                }
            }
            if (!open)
            {
                frmChart chartForm = new frmChart();
                chartForm.MdiParent = this;
                chartForm.Show();
            }
        }
        #endregion

        private void TransactionEntryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActivateTab(TAB_ENTER_TRANSACTION);
        }

        private void tradeOrderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActivateTab(TAB_ENTER_TRADE);
        }

        private void addSecurityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActivateTab(TAB_ADD_SECURITY);
        }

        private void tickerLookupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActivateTab(TAB_TICKER_LOOKUP);
        }

        private void researchTickerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActivateTab(TAB_TICKER_RESEARCH);
        }

        private void ActivateTab(int tab)
        {
            bool found = false;
            foreach (Form frm in this.MdiChildren)
            {
                if (frm is frmMain)
                {
                    found = true;
                    frm.Activate();
                    TabControl tabMain = frm.Controls["tabMain"] as TabControl;
                    if (tabMain != null)
                    {
                        tabMain.SelectTab(tab);
                    }
                }
            }
            if (!found)
            {
                frmMain frm = new frmMain();
                frm.MdiParent = this;
                TabControl tabMain = frm.Controls["tabMain"] as TabControl;
                if (tabMain != null)
                {
                    tabMain.SelectTab(tab);
                }
                frm.Show();
            }
        }

        
        
    }
}
